package com.dmm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.Exception.Exceptionbean;
import com.dmm.result.Result;
import com.dmm.user.entity.Apply;
import com.dmm.user.entity.Signin;
import com.dmm.user.service.ApplyService;
import com.dmm.user.service.SigninService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com/dmm
 * @since 2023-04-03
 */
@RestController
@RequestMapping("/user/apply")
@CrossOrigin
public class ApplyController {

    @Autowired
    ApplyService applyService;

    @Autowired
    SigninService signinService;


    @PutMapping("applyadmin")
    public Result applyadmin(@RequestBody Apply apply){

        applyService.applyadmin(apply);

        return Result.success(null);

    }


    @GetMapping("getinfo")
    public Result getinfo(HttpServletRequest request){

        String email = request.getHeader("token");
        QueryWrapper<Apply> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        try {
            Apply one = applyService.getOne(queryWrapper);
            if(one.getResult()==2){
                one.setResultstr("待审批");
            }else{
                if(one.getResult()==3){
                    one.setResultstr("申请成功");
                }else {
                    one.setResultstr("申请失败");
                }

            }

            Map<String,Object> map=new HashMap<>();
            map.put("result",one.getResult());
            List list=new ArrayList();
            list.add(one);
            map.put("list",list);
            return Result.success(map);
        }catch (Exception e){
            throw new Exceptionbean(20001,"你还没有进行申请");
        }

    }

    @DeleteMapping("removeinfo")
    public Result removeinfo(@RequestBody List<Apply> list){
        QueryWrapper<Apply> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",list.get(0).getEmail());
        applyService.remove(queryWrapper);
        return Result.success(null);
    }


    @ApiOperation("分页查询")
    @PostMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherVo", value = "查询对象", required = false)
            // required = false传入参数可以为空
            @RequestBody(required = false) Apply apply) {
        //创建page对象，传递当前页和每页记录数
        Page<Apply> pageParam = new Page<>(page, limit);
        //获取条件值
        String name = apply.getName();//用户名
        String email = apply.getEmail();//用户邮箱
        String content = apply.getContent();//申请内容

        //封装条件
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(email)) {
            wrapper.eq("email",email);
        }
        if(!StringUtils.isEmpty(content)) {
            wrapper.like("content",content);
        }

        //调用方法得到分页查询结果
        IPage<Apply> pageModel = applyService.page(pageParam, wrapper);
        return Result.success(pageModel);
    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("/deleteId/{id}")
    public Result deleteId(@PathVariable Long id){

        boolean b = applyService.removeById(id);
        if(b){
            return Result.success(null);
        }
        return Result.fail(null);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/Batch_delete")
    public Result Batch_delete(@RequestBody List<Long> list){

        System.out.println(list);
        boolean b = applyService.removeByIds(list);

        return b? Result.success(null):Result.fail(null);
    }

    @ApiOperation("同意成为管理员")
    @DeleteMapping("/okById/{id}")
    public Result okById(@PathVariable Long id,HttpServletRequest request){

        String email=request.getHeader("token");
        boolean b = applyService.removeById(id);
        QueryWrapper<Signin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        Signin one = signinService.getOne(queryWrapper);
        one.setAuthority(1);
        signinService.update(one,queryWrapper);
        if(b){
            return Result.success(null);
        }
        return Result.fail(null);
    }


}

